package com.xbs.dao.content;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 订单dao层
 */
import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.CarEntity;
import com.xbs.entity.CarModelEntity;
import com.xbs.entity.CommonOrderEntity;
import com.xbs.entity.OrderMoney;
import com.xbs.entity.RentEntity;
import com.xbs.entity.TimeEntity;

public class OrderDao extends BaseDao {
	//实例化一个车型对象
	private RowDatas<CarModelEntity> car;
	//实例化一个订单表对象
	private RowDatas<CommonOrderEntity> rent;
	//实例化一个String对象
	private RowDatas<String> str;
	//实例化一个Double类型
	private RowDatas<Double> db;
	public OrderDao() {
		car = new getCar();
		rent = new setCommonOrder();
		str = new getString();
		db = new getServiceMoney();
	}
	
	/**
	 * 1.根据车名字查出该车的配置信息
	 * @param name
	 * @return
	 */
	public CarModelEntity getCarModel(String name){
		String sql = "select * from tb_car_model where car_model_name=?";
		return query(sql, new ParamSet(name), car);
	}
	/**
	 * 2.生成普通订单表(上门取还)
	 * @param order
	 * @return
	 */
	public int insCommonOrder(CommonOrderEntity order){
		String sql = "insert into tb_common_order (co_id,co_rent_idcard,co_car_type,"
				+ "co_get_address,co_return_address,co_order_status,co_order_kind,"
				+ "co_plan_pay_money,co_get_kind) values(?,?,?,?,?,?,?,?,?)" ;
		return updateExecuted(sql, new ParamSet(order.getCo_id(),order.getCo_rent_idcard(),order.getCo_car_type(),order.getCo_get_address(),order.getCo_return_address(),order.getCo_order_status(),order.getCo_order_kind(),order.getCo_plan_pay_money(),order.getCo_get_kind()));
	}
	
	/**
	 * 3.生成实际租车人表（用户手机号码，租车人姓名，租车人身份证号码，租车人联系电话，订单编号）
	 * @param rent
	 * @return
	 */
	public int insRent(RentEntity rent){
		String sql = "insert into tb_rent (user_idcard,rent_name,rent_idcard,rent_telphone,co_id) values(?,?,?,?,?)";
		return updateExecuted(sql, new ParamSet(rent.getUser_idcard(),rent.getRent_name(),rent.getRent_idcard(),
				rent.getRent_telephone(),rent.getCo_id()));
	}
	
	/**
	 * 4.生成订单金额表(上门取送)(订单编号，租金，押金，基本保险)
	 * @param om
	 * @return
	 */
	public int insOrderMoney(OrderMoney om){
		String sql = "insert into tb_order_money (order_id,order_rent_money,order_guarantee_money,order_insurance_money) values(?,?,?,?)";
		return updateExecuted(sql, new ParamSet(om.getOrder_id(),om.getOrder_rent_money(),om.getOrder_guarantee_money(),om.getOrder_insurance_money()));
	}
	
	/**
	 * 5.生成操作人时间表（订单编号，预定取车时间，预定还车时间，订单提交时间）
	 * @param time
	 * @return
	 */
	public int insTime(TimeEntity time){
		String sql = "insert into tb_time (time_order_id,time_plan_get,time_plan_return,time_order_submit) values(?,?,?,?)";
		return updateExecuted(sql, new ParamSet(time.getTime_order_id(),time.getTime_plan_get(),time.getTime_plan_return(),time.getTime_order_submit()));
	}
	/**
	 * 6.根据城市查找该城市的门店编号
	 * @param city
	 * @return
	 */
	public String selStoreId(String city){
		String sql = "select store_id from tb_store where store_city=?";
		return query(sql, new ParamSet(city), str);
	}
	/**
	 * 7.生成普通订单表(到店取还)
	 * @param order
	 * @return
	 */
	public int insCommonOrderStore(CommonOrderEntity order){
		String sql = "insert into tb_common_order (co_id,co_rent_idcard,co_car_type,"
				+ "co_get_store,co_return_store,co_order_status,co_order_kind,"
				+ "co_plan_pay_money,co_get_kind) values(?,?,?,?,?,?,?,?,?)" ;
		return updateExecuted(sql, new ParamSet(order.getCo_id(),order.getCo_rent_idcard(),order.getCo_car_type(),order.getCo_get_store(),order.getCo_return_store(),order.getCo_order_status(),order.getCo_order_kind(),order.getCo_plan_pay_money(),order.getCo_get_kind()));
	}
	/**
	 * 8.当异店还车时，获取手续费
	 * @param fromnum
	 * @param tonum
	 * @return
	 */
	public Double selServiceMoney(int fromnum,int tonum){
		String sql = "select sm_service_money from tb_service_money where (sm_get_store_id='"+fromnum+"' and sm_return_store_id='"+tonum+"') or (sm_get_store_id='"+tonum+"' and sm_return_store_id='"+fromnum+"')";
		System.out.println(sql);
		return query(sql, db);
	}
	
	/**
	 * 9.生成订单金额表(到店取还，异店，有手续费)(订单编号，租金，押金，基本保险，手续费)
	 * @param om
	 * @return
	 */
	public int insOrderMoneyService(OrderMoney om){
		String sql = "insert into tb_order_money (order_id,order_rent_money,order_guarantee_money,order_insurance_money,order_service_money) values(?,?,?,?,?)";
		return updateExecuted(sql, new ParamSet(om.getOrder_id(),om.getOrder_rent_money(),om.getOrder_guarantee_money(),om.getOrder_insurance_money(),om.getOrder_service_money()));
	}
	
	/**
	 * 10.根据车型编号在车表中查车的类型（豪华型等）
	 * @param id
	 * @return
	 */
	public String selCarType(String id){
		String sql="select car_type from tb_car where car_model_id='"+id+"'";
		return query(sql, str);
	}
	
	public class getCar implements RowDatas<CarModelEntity>{
		@Override
		public CarModelEntity getRowData(ResultSet rs) throws SQLException {
			CarModelEntity carmodel = new CarModelEntity();
			carmodel.setCarmodelid(rs.getString("car_model_id"));
			carmodel.setCarmodelname(rs.getString("car_model_name"));
			carmodel.setCarmodeltype(rs.getString("car_model_type"));
			carmodel.setCarmodelyear(rs.getString("car_model_year"));
			carmodel.setSeating(rs.getInt("car_model_seating"));
			carmodel.setDoornumber(rs.getInt("car_model_door_number"));
			carmodel.setFueltype(rs.getString("car_model_fuel_type"));
			carmodel.setCarmodelgps(rs.getString("car_model_gps"));
			carmodel.setCarmodelseat(rs.getString("car_model_seat"));
			carmodel.setCarmodelimage(rs.getString("car_model_image"));
			carmodel.setRentcash(rs.getDouble("car_model_rent_cash"));
			carmodel.setInsurance(rs.getDouble("car_model_insurance"));
			carmodel.setDiscount(rs.getDouble("company_discount"));
			carmodel.setPledge(rs.getDouble("car_model_pledge"));
			return carmodel;
		}
	}
	
	//订单表对象
	public class setCommonOrder implements RowDatas<CommonOrderEntity>{
		@Override
		public CommonOrderEntity getRowData(ResultSet rs) throws SQLException {
			CommonOrderEntity com = new CommonOrderEntity();
			com.setCo_id(rs.getString("co_id"));
			com.setCo_rent_idcard(rs.getString("co_rent_idcard"));
			com.setCo_car_id(rs.getString("ca_card_id"));
			com.setCo_car_type(rs.getString("co_car_type"));
			com.setCo_get_address(rs.getString("co_get_address"));
			com.setCo_return_address(rs.getString("co_return_address"));
			com.setCo_order_status(rs.getString("co_order_status"));
			com.setCo_order_kind(rs.getString("co_order_kind"));
			com.setCo_plan_pay_money(rs.getDouble("co_plan_pay_money"));
			com.setCo_fact_pay_money(rs.getDouble("co_fact_pay_money"));
			com.setCo_return_money(rs.getDouble("co_return_money"));
			com.setCo_compensation(rs.getDouble("co_compensation"));
			com.setCo_get_kind(rs.getString("co_get_kind"));
			com.setCo_time(rs.getString("co_time"));
			com.setCo_get_store(rs.getString("co_get_store"));
			com.setCo_return_store(rs.getString("co_return_store"));
			com.setCo_get_operator(rs.getString("co_get_operator"));
			com.setCo_return_operator(rs.getString("co_return_operator"));
			com.setCo_remark(rs.getString("co_remark"));
			com.setCo_first_pay_money(rs.getDouble("co_first_pay_money"));
			return com;
		}
	}
	//获取城市编号
	public class getString implements RowDatas<String>{
		@Override
		public String getRowData(ResultSet rs) throws SQLException {
			return rs.getString("store_id");
		}
	}
	
	//当异店还车时，获取手续费
	public class getServiceMoney implements RowDatas<Double>{
		@Override
		public Double getRowData(ResultSet rs) throws SQLException {
			return rs.getDouble("sm_service_money");
		}
	}
}
